package com.android.view.activity.user;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.model.entity.eventbus.BusUtils;
import com.android.model.info.LocalSp;
import com.android.util.SwitchLanguageUtil;
import com.android.util.log.LogUtil;
import com.android.view.activity.base.ToolbarBaseActivity;
import com.android.R;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;

/**
 * 设置语言
 * Created by TenFace on 2018/4/26.
 */

public class SettingLanguageActivity extends ToolbarBaseActivity {

    private Context context;

    @BindView(R.id.ivChinese)
    ImageView ivChinese;
    @BindView(R.id.llChinese)
    LinearLayout llChinese;
    @BindView(R.id.ivEnglish)
    ImageView ivEnglish;
    @BindView(R.id.llEnglish)
    LinearLayout llEnglish;
    @BindView(R.id.tvChinese)
    TextView tvChinese;
    @BindView(R.id.tvEnglish)
    TextView tvEnglish;

    private String languageStatus;
    private String languageString = "";

//    @Override
//    public void onBackPressed() {
//        if (!StringUtils.isEmpty(languageString)) {
//            Intent intent = new Intent();
//            intent.putExtra(CommonType.MYSELF_SETTING_LANGUAGE_STATUS, languageString);
//            setResult(1, intent);
//            finish();
//        }
//    }

    @Override
    protected void setContentLayout() {
        setContentView(R.layout.activity_setting_language);
    }

    @Override
    protected void initView() {
        context = this;
        languageStatus = LocalSp.getInstance(context).getLanguageStatus();
        LogUtil.e(languageStatus);
        switch (languageStatus) {
            case "zh":
                ivChinese.setBackgroundResource(R.mipmap.color_preference_checked);
                ivEnglish.setBackgroundResource(R.color.transparent);
                languageString = tvChinese.getText().toString();
                break;
            case "en":
                ivChinese.setBackgroundResource(R.color.transparent);
                ivEnglish.setBackgroundResource(R.mipmap.color_preference_checked);
                languageString = tvEnglish.getText().toString();
                break;
            default:
                //默认是英语
                ivChinese.setBackgroundResource(R.color.transparent);
                ivEnglish.setBackgroundResource(R.mipmap.color_preference_checked);
                languageString = tvEnglish.getText().toString();
        }

    }

    @Override
    protected void obtainData() {

    }

    @Override
    protected void initEvent() {

        llChinese.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                ivChinese.setBackgroundResource(R.mipmap.color_preference_checked);
                ivEnglish.setBackgroundResource(R.color.transparent);
                languageString = tvChinese.getText().toString();
                LocalSp.getInstance(context).saveLanguageStatus("zh");
                SwitchLanguageUtil.changeLanguage(context, "zh");
                BusUtils.SwitchLanguage switchLanguage = new BusUtils.SwitchLanguage();
                switchLanguage.setLanguage("zh");
                EventBus.getDefault().post(switchLanguage);

            }
        });

        llEnglish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                ivChinese.setBackgroundResource(R.color.transparent);
                ivEnglish.setBackgroundResource(R.mipmap.color_preference_checked);
                languageString = tvEnglish.getText().toString();
                LocalSp.getInstance(context).saveLanguageStatus("en");
                SwitchLanguageUtil.changeLanguage(context, "en");
                BusUtils.SwitchLanguage switchLanguage = new BusUtils.SwitchLanguage();
                switchLanguage.setLanguage("en");
                EventBus.getDefault().post(switchLanguage);

            }
        });
    }
}
